package com.roocon.thread.ExceptionHandler;

public class UncaughtExceptionHandlerDemo {

    public static void main(String[] args) {
        // 设置默认的未捕获异常处理器
        Thread.setDefaultUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());

        // 创建一个线程，该线程会抛出一个未捕获的异常
        Thread thread = new Thread(() -> {
            System.out.println("This thread will throw an exception...");
            throw new RuntimeException("Oops! An error occurred.");
        });

        // 启动线程
        thread.start();
    }
}

class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("Exception caught in thread: " + t.getName());
        System.out.println("Exception message: " + e.getMessage());
        // 这里可以添加其他的异常处理逻辑，例如记录日志、发送错误报告等
    }
}
